package com.siblings.loan.app

import android.annotation.SuppressLint
import android.content.Context
import androidx.core.content.ContextCompat
import com.lib.base.base.BaseApplication
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.siblings.loan.R
import com.siblings.loan.utils.helper.SibAnalysisHelper
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.http.OkHttpLoader

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/3 14:20
 * @Annotation：
 */
class Apps : BaseApplication(){

    companion object {
        // 全局Context
        @SuppressLint("StaticFieldLeak")
        lateinit var context: Context

        @SuppressLint("StaticFieldLeak")
        lateinit var app: Apps

        //static 代码段可以防止内存泄露
        init {
            //设置全局的Header构建器
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
                layout.setPrimaryColorsId(com.lib.base.R.color.transparent, R.color.theme) //全局设置主题颜色
                MaterialHeader(context).setColorSchemeColors(ContextCompat.getColor(context,R.color.theme)) //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
            //设置全局的Footer构建器
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ -> //指定为经典Footer，默认是 BallPulseFooter
                ClassicsFooter(context).setDrawableSize(20f)
            }
        }
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        context = base
        app = this
    }

    override fun onCreate() {
        super.onCreate()

        initLibConfigs()
    }

    fun getAppContext(): Context{
        return context
    }

    /**
     * 初始化Lib
     */
    private fun initLibConfigs(){
        HttpHelper.init(OkHttpLoader())
        //初始化 analysis
        SibAnalysisHelper.initAnalysisLibAtAppLaunch()
    }

    /**
     * 获取当前手机屏幕的宽
     */
    fun getPhoneWidth(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.widthPixels
    }

    /**
     * 获取当前手机屏幕的高
     */
    fun getPhoneHeight(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.heightPixels
    }
}